//
//  TravelDetailController.m
//  XhlTrip
//
//  Created by dllo on 15/8/22.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TravelDetailController.h"
#import "TravelDetailCell.h"
#import "LeaonWorking.h"
#import "TraDetaHeaView.h"
#import "TravelModel.h"
#import "UIImageView+WebCache.h"
#import "DataBase.h"
#import "DBModel.h"
#import "MBProgressHUD.h"
#import "SDProgressView.h"

@interface TravelDetailController ()<UITableViewDataSource, UITableViewDelegate>

{
    CGFloat he;
    int que;
}

@property (nonatomic, retain) NSMutableDictionary *travelDic;
@property (nonatomic, retain) NSMutableArray *travelDayArr;
@property (nonatomic, retain) UITableView *tableViewDetil;
@property (nonatomic, retain) TraDetaHeaView *headerView;
@property (nonatomic, retain) UIView *navi;

@property (nonatomic, retain) UIImageView *naviImage;
@property (nonatomic, retain) UILabel *dayLable;
@property (nonatomic, retain) UIButton *shoucang;
@property (nonatomic, retain) UIButton *backButton;
@property (nonatomic, retain) UIImageView *bigImage;
@property (nonatomic, retain) UIVisualEffectView * effectview;
@property (nonatomic, retain)UIButton *xiaButton;
@property (nonatomic, retain)UIView *tanView;
@property (nonatomic, retain)UITableView *tantable;
@property (nonatomic, retain)UILabel *xingcheng;
@property (nonatomic, retain)UIButton *dituButton;
@property (nonatomic, retain)NSMutableArray * didianArr;
@property (nonatomic, retain)NSMutableDictionary *dimingDic;
@property (nonatomic, copy)NSString *pian;
@property (nonatomic, retain)UIView *navView;
@property (nonatomic, retain)UIScrollView *scroll;
@property (nonatomic, retain)UIView *scTopView;
@property (nonatomic, retain)UIView *scDownView;

@property (nonatomic, retain) UIImageView *SmallImageView;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *datalabel;
@property (nonatomic, retain) UILabel *daysLabel;
@property (nonatomic, retain) UILabel *pCountLabel;

@property (nonatomic, retain) UIActivityIndicatorView *acticity;



@property (nonatomic, retain) TravelModel *travelModel;



@end

@implementation TravelDetailController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
        self.travelDayArr = [NSMutableArray array];
        self.didianArr = [NSMutableArray array];
        self.dimingDic = [NSMutableDictionary dictionary];
    }
    return self;
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];

    
   [self createScrollView];
   [self createXiaButton];
   [self createTanTableView];
   
}

// 创建scrollView
-(void)createScrollView
{
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height + 50)];
    self.scroll.contentSize = CGSizeMake(0, self.view.frame.size.height + 50 + 200 - 64);
    self.scroll.delegate = self;
    self.scroll.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scroll];
    [_scroll release];
    
    self.scTopView = [[UIView alloc]initWithFrame:CGRectMake(-100, -50 * Height, self.view.frame.size.width + 200 * Width, 350 * Height)];
    [self.scroll addSubview:self.scTopView];
    [_scTopView release];
    self.bigImage = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, self.scTopView.frame.size.width , self.scTopView.frame.size.height)];
    self.bigImage.userInteractionEnabled = YES;
    [self.scTopView addSubview:self.bigImage];
    [_bigImage release];
    
    self.SmallImageView = [[UIImageView alloc]initWithFrame:CGRectMake(110, 250 *Height , 40 *Width, 40*Width)];
    self.SmallImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.SmallImageView.layer.borderWidth = 1;
    self.SmallImageView.layer.cornerRadius = 20 * Width;
    self.SmallImageView.layer.masksToBounds = YES;
    [self.bigImage addSubview:self.SmallImageView];
    [_SmallImageView release];
    

    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(160 * Width, 250 * Height, 290 * Width , 20 *Height)];
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.font = [UIFont fontWithName:@"Verdana-BoldItalic" size:14];
    [self.bigImage addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.datalabel = [[UILabel alloc]initWithFrame:CGRectMake(160 * Width, 270 * Height, 100 * Width, 20 * Height)];
    self.datalabel.textColor = [UIColor whiteColor];
    self.datalabel.font = [UIFont fontWithName:@"Verdana-BoldItalic" size:12];
    [self.bigImage addSubview:self.datalabel];
    [_datalabel release];
    
    self.daysLabel = [[UILabel alloc]initWithFrame:CGRectMake(250 * Width, 270 *Height, 40 * Width, 20 * Height)];
    self.daysLabel.textColor = [UIColor whiteColor];
    self.daysLabel.font = [UIFont fontWithName:@"Verdana-BoldItalic" size:12];
    [self.bigImage addSubview:self.daysLabel];
    [_datalabel release];
    
    self.pCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(290 * Width, 270 *Height, 60 *Width, 20 *Height)];
    self.pCountLabel.textColor = [UIColor whiteColor];
    self.pCountLabel.font = [UIFont fontWithName:@"Verdana-BoldItalic" size:12];
    [self.bigImage addSubview:self.pCountLabel];
    [_pCountLabel release];
    
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    self.effectview.alpha = 0;
    self.effectview.frame = CGRectMake(0, 0, self.bigImage.frame.size.width, self.bigImage.frame.size.height);
    [self.bigImage addSubview:self.effectview];
    
    
    
    self.scDownView = [[UIView alloc]initWithFrame:CGRectMake(0, 250 * Height, self.view.frame.size.width, self.scroll.frame.size.height - 64 * Height)];
    self.scDownView.userInteractionEnabled = NO;
    [self.scroll addSubview:self.scDownView];
    [_scDownView release];
    
    self.tableViewDetil = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.scDownView.frame.size.height) style:UITableViewStylePlain];
    self.tableViewDetil.backgroundColor = [UIColor clearColor];
    self.tableViewDetil.dataSource = self;
    self.tableViewDetil.delegate = self;
    self.tableViewDetil.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.scDownView addSubview:self.tableViewDetil];
    _tableViewDetil.separatorColor = [UIColor grayColor];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hui:)];
    [self.tableViewDetil addGestureRecognizer:tap];
    
    [_tableViewDetil release];
    _tableViewDetil.backgroundColor = [UIColor whiteColor];
    
}

// 创建下边button
-(void)createXiaButton
{
    self.navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    self.navView.backgroundColor = [UIColor grayColor];
    self.navView.alpha = 0.3;
    [self.view addSubview:self.navView];
    [_navView release];

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(5, 20, 40, 40);
    [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    [self.navView addSubview:backButton];
    
    self.xiaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.xiaButton.frame = CGRectMake(10, 667 * Height, 40*Width, 40 *Width);
    self.xiaButton.layer.cornerRadius = 20 * Width;
    self.xiaButton.layer.borderWidth = 1;
    self.xiaButton.layer.borderColor = [UIColor whiteColor].CGColor;
    self.xiaButton.layer.masksToBounds = YES;
    [self.xiaButton setImage:[UIImage imageNamed:@"fenlei5.png"] forState:UIControlStateNormal];
    [self.xiaButton addTarget:self action:@selector(tan:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.xiaButton];
    
    self.tanView = [[UIView alloc]initWithFrame:CGRectMake(-240 * Width, 64 * Height, 240 * Width, self.view.frame.size.height - 64  *Height)];
    self.tanView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.tanView];
    
    self.xingcheng = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 120  *Width, 40 * Height)];
    self.xingcheng.text = @"行程概览";
    self.xingcheng.textAlignment = NSTextAlignmentCenter;
    self.xingcheng.textColor = [UIColor whiteColor];
    [self.tanView addSubview:self.xingcheng];
    [_xingcheng release];
    
    self.dituButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.dituButton.frame = CGRectMake(120 * Width, 0, 120 * Width, 40 * Height);
    [self.dituButton setTitle:@"地图模式" forState:UIControlStateNormal];
    [self.dituButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.dituButton.alpha = 0.8;
    [self.tanView addSubview:self.dituButton];
    
    [_tanView release];
}

// 创建点击下边button弹出来的tableView
-(void)createTanTableView
{
    self.tantable = [[UITableView alloc]initWithFrame:CGRectMake(0, 40 * Height, self.tanView.frame.size.width, self.tanView.frame.size.height - 40 * Height) style:UITableViewStyleGrouped];
    self.tantable.delegate = self;
    self.tantable.dataSource = self;
    self.tantable.backgroundColor = [UIColor blackColor];
    self.tantable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tanView addSubview:self.tantable];
    [_tantable release];
}


// 下边button点击事件
-(void)tan:(UIButton *)button
{
    self.scDownView.userInteractionEnabled = YES;
    [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
        [self.tanView setFrame:CGRectMake(0, 64, 240, self.view.frame.size.height - 64)];
        self.tanView.alpha = 0.8;
        [self.xiaButton setFrame:CGRectMake(10, 667 * Height, 40*Width, 40 *Width)];
    } completion:^(BOOL finished) {
        
    }];
}

// 网络请求
- (void)passDetailValue:(NSString *)idStr
{
#if 1
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
    
    view.backgroundColor = [UIColor clearColor];
    
    
    [self.view addSubview:view];
    
    [view release];
    
#pragma mark - 小圆圈
    
    self.acticity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30, (self.view.frame.size.height - 64) / 2 - 30, 60, 60)];
    
    self.acticity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    
    [view addSubview:self.acticity];
    
    [self.acticity startAnimating];
    
    [_acticity release];
#endif
    NSString *path = [NSString stringWithFormat:@"%@%@%@",@"https://chanyouji.com/api/trips/", idStr, @".json"];
    NSString *nn = [NSString stringWithFormat:@"%@%@", @"travel", idStr];
    [LeaonWorking netWorkingHanderGetGataWithUrl:path pathname:nn resultBlock:^(id result) {
        self.travelDic  = result;
        [self.travelDayArr addObjectsFromArray:[self.travelDic objectForKey:@"trip_days"]];
        [self.SmallImageView sd_setImageWithURL:[[self.travelDic objectForKey:@"user"] objectForKey:@"image"]];
        self.nameLabel.text = [self.travelDic objectForKey:@"name"];
        self.datalabel.text = [self.travelDic objectForKey:@"end_date"];
        NSString *days = [NSString stringWithFormat:@"%@%@%@", @"/", [self.travelDayArr.lastObject objectForKey:@"day"], @"天"];
        self.daysLabel.text = days;
        
        NSString *tu = [NSString stringWithFormat:@"%@%@%@", @"/" ,[self.travelDic objectForKey:@"photos_count"], @"图"];
        self.pCountLabel.text = tu;
        
        [self.bigImage sd_setImageWithURL:[self.travelDic objectForKey:@"front_cover_photo_url"] ];
        NSLog(@"%@", [self.travelDic objectForKey:@"front_cover_photo_url"]);
        
        
        
        for (int i = 0; i < self.travelDayArr.count; i++) {
            NSMutableArray *arr = [NSMutableArray array];
            [arr addObjectsFromArray:[[self.travelDayArr objectAtIndex:i] objectForKey:@"nodes"]];
            
            NSMutableArray * mingzi = [NSMutableArray array];
            for (int j = 0; j < arr.count; j++) {
                NSString *str = [NSString stringWithFormat:@"%@", [[arr objectAtIndex:j] objectForKey:@"entry_name"]];
                NSLog(@"str   ===  %@", str);
                if ([str isEqualToString:@"<null>"]) {
                    NSLog(@"sfsadfasdfsadfasdf");
                    
                }else{
                    que = j;
                    NSLog(@"%d", que);
                    [mingzi addObject:str];
                    NSLog(@"%@", mingzi);
                }
            }
            [self.didianArr addObject:mingzi];
        }
        
        NSLog(@"dididid%@", self.didianArr);
        
        
        for (int i = 0; i < self.travelDayArr.count; i++) {
            
            NSMutableArray * arr = [NSMutableArray array];
            NSString *sec = [NSString stringWithFormat:@"%d", i];
            [self.dimingDic setObject:arr forKey:sec];
           
            NSArray *a = [self returnArrayThatYouNeed:[[self.travelDayArr objectAtIndex:i] objectForKey:@"nodes"]];
            
            CGFloat textHeight = 0.0;
            CGFloat photoHeight;
            
            for (int j = 0; j < a.count; j++) {
                if ([a[j] objectForKey:@"photo"]) {
                    photoHeight = [TravelDetailCell heightWithImage:[a[j] objectForKey:@"photo"] width:self.view.frame.size.width - 20];
                }else
                {
                    photoHeight = 0;
                }
                
                if ([[[a objectAtIndex:j] objectForKey:@"description"] isKindOfClass:[NSNull class]]) {
                    textHeight = 0;
                }else{
                    
                    textHeight = [TravelDetailCell heightWithText:[a[j] objectForKey:@"description"] width:self.view.frame.size.width - 20];
                }
                
                NSMutableDictionary * dic = [NSMutableDictionary dictionary];
                
                he += textHeight + photoHeight + 40 * Height;
                NSString *secotion = [NSString stringWithFormat:@"%d", i];
                NSString *row = [NSString stringWithFormat:@"%d", j];
                
                NSString * heStr = [NSString stringWithFormat:@"%f", he];
                
                [dic setObject:heStr forKey:row];
                [[self.dimingDic objectForKey:secotion] addObject:dic];
                
            }
            
            
        }
        
#pragma mark - 数据请求之后tableview重新加载数据
        self.tableViewDetil.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [_tableViewDetil reloadData];
        
        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
            [self.xiaButton setFrame:CGRectMake(10 , 620*Height, 40 * Width, 40 *Width)];
        } completion:^(BOOL finished) {
            
        }];
        
        [self.acticity stopAnimating];
        [view removeFromSuperview];
        
        [_tantable reloadData];
        he = 0;
        
    }];
    
}


// 返回button
-(void)back:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


// 点击手势, 点击屏幕让弹出来的tableView回去
-(void)hui:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
        [self.tanView setFrame:CGRectMake(-240 * Width, 64 * Height, 240 * Width, self.view.frame.size.height - 64  *Height)];
        self.tanView.alpha = 1;
        self.xiaButton.frame = CGRectMake(10 , 620*Height, 40 * Width, 40 *Width);
    } completion:^(BOOL finished) {
        
    }];

}

#if 1
/* 分区数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.travelDayArr.count;
}
#endif

/* 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableViewDetil) {
        NSArray *cellArr = [self returnArrayThatYouNeed:[self.travelDayArr[section] objectForKey:@"nodes"]];
        return cellArr.count;

    }else{
        NSMutableArray * arr = [self.didianArr objectAtIndex:section];
        return arr.count;
    }
}

/* 整理数据 */

- (NSMutableArray *)returnArrayThatYouNeed:(NSMutableArray *)nodes {
    NSMutableArray *newArray = [NSMutableArray array];
    for (NSDictionary *nodeDic in nodes) {
        NSMutableArray *notes = [NSMutableArray array];
        notes = [nodeDic objectForKey:@"notes"];
        for (NSDictionary *noteDic in notes) {
            [newArray addObject:noteDic];
        }
    }
    return newArray;
}


/* 创建cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableViewDetil) {
        static NSString *reuse = @"ss";
        TravelDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        
        if (cell == nil) {
            cell = [[[TravelDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
        }
        /* 从重用池去除视图并清除上面的子视图 */
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
        }
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray *a = [self returnArrayThatYouNeed:[self.travelDayArr[indexPath.section] objectForKey:@"nodes"]];
        [cell passValue:[a objectAtIndex:indexPath.row]];
        
        
        
        return cell;

    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hh"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hh"];
        }
        cell.textLabel.text = [[self.didianArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.backgroundColor = [UIColor blackColor];
        return cell;
    }
}

/* cell 高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (tableView == self.tableViewDetil) {
        NSArray *a = [self returnArrayThatYouNeed:[self.travelDayArr[indexPath.section] objectForKey:@"nodes"]];
        
        CGFloat textHeight;
        CGFloat photoHeight;
        if ([a[indexPath.row] objectForKey:@"photo"]) {
            photoHeight = [TravelDetailCell heightWithImage:[a[indexPath.row] objectForKey:@"photo"] width:self.view.frame.size.width - 20];
        }else
        {
            photoHeight = 0;
        }
        
        if ([[[a objectAtIndex:indexPath.row] objectForKey:@"description"] isKindOfClass:[NSNull class]]) {
            textHeight = 0;
        }else{
            
            textHeight = [TravelDetailCell heightWithText:[a[indexPath.row] objectForKey:@"description"] width:self.view.frame.size.width - 20];
        }
        
        return textHeight + photoHeight + 40 * Height;
    }else{
        return 30;
    }
 
    
      
}

// 区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == self.tableViewDetil) {
        NSNumber *num = [[self.travelDayArr objectAtIndex:section] objectForKey:@"day"];
        NSString *ff = [NSString stringWithFormat:@"%@%@",  @"DAY", num];
        return ff;
    }else{
        NSNumber *num = [[self.travelDayArr objectAtIndex:section] objectForKey:@"day"];
        NSString *ff = [NSString stringWithFormat:@"%@%@",  @"。DAY", num];
        return ff;
    }
   
}

// 地名的tableView点击cell触发事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@", self.dimingDic);
    
    [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
        [self.scroll setContentOffset:CGPointMake(0, 200 - 64)];
        
    } completion:^(BOOL finished) {
        
    }];
    
    
    if (tableView == self.tantable) {
        int count = 0;
        NSLog(@"%ld", indexPath.section);
        NSString *ming = [[self.didianArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        NSLog(@"ming = %@", ming);
        
            NSMutableArray *arr = [NSMutableArray array];
            [arr addObjectsFromArray:[[self.travelDayArr objectAtIndex:indexPath.section] objectForKey:@"nodes"]];
            for (int j = 0; j < arr.count; j++) {
                NSString *str = [NSString stringWithFormat:@"%@", [[arr objectAtIndex:j] objectForKey:@"entry_name"]];
                if ([str isEqualToString:ming]) {
                    que = j;
                    NSLog(@"  que =  %d", que);
                    break;
                    
                }
            }

        
        NSMutableArray *arr111 = [NSMutableArray array];
        [arr111 addObjectsFromArray:[[self.travelDayArr objectAtIndex:indexPath.section] objectForKey:@"nodes"]];
        for (int x = 0; x < que; x++) {
            NSArray *liceng = [[arr111 objectAtIndex:x] objectForKey:@"notes"];
            count += liceng.count;
            NSLog(@"1111111   %d", count);
        }
        
        if (count == 0) {
            NSString *strQue = [NSString stringWithFormat:@"%d", count];
            NSLog(@"strque = %@", strQue);
            NSString *sec = [NSString stringWithFormat:@"%ld", indexPath.section];
            NSLog(@"sec = %@", sec);
            NSArray * arr = [self.dimingDic objectForKey:sec];
            NSLog(@"%@", arr);
            for (NSDictionary *dic in arr) {
                for (NSString *key in dic) {
                    if ([key isEqualToString:strQue]) {
                        NSLog(@"key = %@", key);
                        self.pian = [NSString stringWithFormat:@"%@", [dic objectForKey:key]];
                        NSLog(@"%@", self.pian);
                    }
                }
            }
        }else{
            NSString *strQue = [NSString stringWithFormat:@"%d", count - 1];
            NSLog(@"strque = %@", strQue);
            NSString *sec = [NSString stringWithFormat:@"%ld", indexPath.section];
            NSLog(@"sec = %@", sec);
            NSArray * arr = [self.dimingDic objectForKey:sec];
            NSLog(@"%@", arr);
            for (NSDictionary *dic in arr) {
                for (NSString *key in dic) {
                    if ([key isEqualToString:strQue]) {
                        NSLog(@"key = %@", key);
                        self.pian = [NSString stringWithFormat:@"%@", [dic objectForKey:key]];
                        NSLog(@"%@", self.pian);
                    }
                }
            }
        }
        
        
        
        
        CGFloat pianyi = [self.pian floatValue];
        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
            [self.tanView setFrame:CGRectMake(-240 * Width, 64 * Height, 240 * Width, self.view.frame.size.height - 64  *Height)];
            self.tanView.alpha = 1;
            self.xiaButton.frame = CGRectMake(10 , 620*Height, 40 * Width, 40 *Width);
            [self.tableViewDetil setContentOffset:CGPointMake(0, pianyi + indexPath.section * 30)];
        } completion:^(BOOL finished) {
            
        }];
        
    }
    
}

// 区高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

/* 收藏按钮 */
- (void)createSaveButton
{
   
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 30 * Width, 30 * Height);
        [button addTarget:self action:@selector(shoucangButton:) forControlEvents:UIControlEventTouchUpInside];
        NSString *ss = [NSString stringWithFormat:@"%@", [self.travelDic objectForKey:@"id"]];
    
        if ([DataBase selectoneWith:ss] != nil) {
            [button setBackgroundImage:[self nameWithPath:@"shoucangS.png"] forState:UIControlStateNormal];
        }else{
            [button setBackgroundImage:[self nameWithPath:@"shoucang.png"] forState:UIControlStateNormal];
        }
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
       
}

/* 收藏点击事件 */
- (void)shoucangButton:(UIButton *)button
{
    
    NSString *ss = [NSString stringWithFormat:@"%@", [self.travelDic objectForKey:@"id"]];
    
    if ([DataBase selectoneWith:ss] != nil)  {
        [button setBackgroundImage:[UIImage imageNamed:@"shoucang.png"] forState:UIControlStateNormal];
        
        DBModel *model = [DBModel dbModelWith:[self.travelDic objectForKey:@"name"] photos_count:[self.travelDic objectForKey:@"photos_count"] start_date:[self.travelDic objectForKey:@"start_date"] days:[self.travelDic objectForKey:@"days"] front_cover_photo_url:[self.travelDic objectForKey:@"front_cover_photo_url"] number:[self.travelDic objectForKey:@"id"]];
        [DataBase deleteWithModel:model];
    }else
    {
        [button setBackgroundImage:[UIImage imageNamed:@"shoucangS.png"] forState:UIControlStateNormal];
        
        DBModel *model = [DBModel dbModelWith:[self.travelDic objectForKey:@"name"] photos_count:[self.travelDic objectForKey:@"photos_count"] start_date:[self.travelDic objectForKey:@"start_date"] days:[self.travelDic objectForKey:@"days"] front_cover_photo_url:[self.travelDic objectForKey:@"front_cover_photo_url"] number:[self.travelDic objectForKey:@"id"]];
        [DataBase insertWithModel:model];
    }
}

- (UIImage *)nameWithPath:(NSString *)paths
{
    NSString *path = [[NSBundle mainBundle] pathForResource:paths ofType:nil];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}

#if 1

// 通过偏移量计算滑动效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    if (scrollView == self.tableViewDetil) {
        CGFloat y = self.tableViewDetil.contentOffset.y;
        if (y < 0) {
            self.scDownView.userInteractionEnabled = NO;
        }
    }
    
    if (scrollView == self.scroll) {
        CGFloat pian = scrollView.contentOffset.y;
        NSLog(@"%f", pian);
        self.effectview.alpha = pian / 230;
        
        self.SmallImageView.alpha = (83 - pian) / 100;
        self.nameLabel.alpha = (83 - pian) / 100;
        self.datalabel.alpha = (83 - pian) / 100;
        self.daysLabel.alpha = (83 - pian) / 100;
        self.pCountLabel.alpha = (83 - pian) / 100;
        
        CGFloat y = self.scroll.contentOffset.y;
        if (y >= 200 - 70) {
            self.scDownView.userInteractionEnabled = YES;
            [self.scroll setContentOffset:CGPointMake(0, 200 - 64)];
            
        }else if (y == 0) {
            self.scDownView.userInteractionEnabled = NO;
        }else if (y < -70){
            [self.scroll setContentOffset:CGPointMake(0, -70)];
        }

    }
    
        
        NSLog(@"%f", scrollView.contentOffset.y);

    
}

#endif



- (void)dealloc
{
    
    [_headerView release];
    [_tableViewDetil release];
    [_travelDayArr release];
    [_travelDic release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
   
    self.tabBarController.tabBar.hidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
